/*
    Android UIML

	Copyright (C) 2010  Bram Goffings (bramgoffings@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public License
	as published by the Free Software Foundation; either version 2.1
	of	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package uiml.android.main;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import uiml.android.elements.UimlDocument;
import android.app.Activity;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;

public class AndroidUiml extends Activity {
	
	 public static Activity me = null; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        
        // Make the context avalaible for every class
        me = this; 
        
    	addContentView(listExamples(), new ViewGroup.LayoutParams(-1, -1));    
    }  
    
    public void onStop(){
    	setContentView(listExamples(), new ViewGroup.LayoutParams(-1, -1));
        super.onStop();
    }
    
    private View listExamples() {
    	
    	ScrollView scroller = new ScrollView(AndroidUiml.me);
    	scroller.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    	
    	LinearLayout layout = new LinearLayout(AndroidUiml.me);
    	layout.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    	layout.setOrientation(1);

        RadioGroup rGroup = new RadioGroup(AndroidUiml.me);
    	rGroup.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    	
    	addRadioButton(rGroup, "spinner");	
    	addRadioButton(rGroup, "copy_text");
    	addRadioButton(rGroup, "concat_strings");
    	addRadioButton(rGroup, "image_view");
    	addRadioButton(rGroup, "image_gallery");
    	addRadioButton(rGroup, "rating_bar");
    	addRadioButton(rGroup, "radio_button_complex");
    	addRadioButton(rGroup, "web_view");	
    	addRadioButton(rGroup, "seek_bar");	
    	addRadioButton(rGroup, "seek_bar2");
    	addRadioButton(rGroup, "survey_app");
    	addRadioButton(rGroup, "table_layout");
    	addRadioButton(rGroup, "table_layout2");	
    	
    	layout.addView(rGroup);
    	scroller.addView(layout);
    	
    	
    	return scroller;
    }
    
    private void addRadioButton(RadioGroup rGroup, CharSequence widget) {
    	RadioButton r = new RadioButton(AndroidUiml.me);
    	r.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    	r.setText(widget);
    	rGroup.addView(r);
    	r.setOnClickListener(getRadioListener());
    }
    
    private View.OnClickListener getRadioListener() {
    	View.OnClickListener radio_listener = new View.OnClickListener(){
    		
			public void onClick(View v){ 
				RadioButton r = (RadioButton) (v);
				String widget = (String) (r.getText());

				String pack = AndroidUiml.me.getClass().getPackage().getName();
				int resID = AndroidUiml.me.getResources().getIdentifier(widget, "raw", pack);
				
				try {				
					UimlDocument root = new UimlDocument(getResources().openRawResource(resID));
					setContentView(root.render());
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (NotFoundException e) {
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (SAXException e) {
					e.printStackTrace();
				}		
			}
		};
		
		return radio_listener;
    }

}